﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using VPSoftTestApplication.Common;
using VPSoftTestApplication.Entity;
using VPSoftTestApplication.Presenters;

namespace VPSoftTestApplication.View
{
    public partial class MainForm : Form, IMainView
    {
        private int _countPage;
        private const int pageSize = 10;
        private int _currentPage = 1;

        public MainForm()
        {
            InitializeComponent();
            InventoryBindingSource.DataSource = new CustomBindingList<Inventory>(new List<Inventory>(), false);
            DocumentHeaderBindingSource.DataSource = new CustomBindingList<DocumentHeader>(new List<DocumentHeader>(), true);
            new MainViewPresenter(this);
            
        }

        private void changeDbButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (dbOpenFileDialog.ShowDialog() != DialogResult.OK)
                    return;
                ChangeDatabase(null, null);
                inventoryBindingNavigator.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                inventoryBindingNavigator.Enabled = false;
            }
        }
        
        private void DocumentHeaderBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            try
            {
                LoadInventors(null, null);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private void inventoryDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex > 0)
                    EditInventory(null, null);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void inventoryDataGridView_Sorted(object sender, EventArgs e)
        {
            try
            {
                LoadInventors(null, null);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void inventoryDataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                DataGridViewColumn col = inventoryDataGridView.Columns[e.ColumnIndex];
                if (col.Name == "DeleteColumn")
                {
                    if (
                        MessageBox.Show("Are tou shure?", string.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                                        MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                        DeleteInventory(null, null);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void bindingNavigatorMoveNextPage_Click(object sender, EventArgs e)
        {
            if (Page<_countPage)
                Page++;
        }

        private void bindingNavigatorMovePreviousPage_Click(object sender, EventArgs e)
        {
            if (Page>1)
                Page--;
        }

        private void bindingNavigatorMoveLastPage_Click(object sender, EventArgs e)
        {
            Page = _countPage;
        }

        private void bindingNavigatorMoveFirstPage_Click(object sender, EventArgs e)
        {
            Page = 1;
        }

        private void bindingNavigatorPositionPage_TextChanged(object sender, EventArgs e)
        {
            int page;
            if (int.TryParse(bindingNavigatorPositionPage.Text, out page))
            {
                Page = page;
            }
        }

        private void bindingNavigatorPositionPage_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = true;
            if (e.KeyCode == Keys.Back ||
                    e.KeyCode == Keys.Delete ||
                    e.KeyCode == Keys.Left ||
                    e.KeyCode == Keys.Right)
            {
                e.SuppressKeyPress = false;
                return;
            }


            if (e.KeyCode < Keys.D0 ||
                e.KeyCode > Keys.D9)
            {
                if (e.KeyCode < Keys.NumPad0 ||
                    e.KeyCode > Keys.NumPad9)
                    return;
            }

            KeysConverter conv = new KeysConverter();
            string keyString = conv.ConvertToString(e.KeyCode);
            if (keyString.Length > 1)
                keyString = keyString[keyString.Length - 1].ToString();
            int prevPage = int.Parse(bindingNavigatorPositionPage.Text + keyString);
            if (prevPage > _countPage || prevPage < 1)
            {
                return;
            }
            e.SuppressKeyPress = false;

        }

        private void bindingNavigatorPositionPage_Leave(object sender, EventArgs e)
        {
            int page;
            if (!int.TryParse(bindingNavigatorPositionPage.Text, out page))
            {
                bindingNavigatorPositionPage.Text = Page.ToString();
            }

        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            try
            {
                AddInventory(null, null);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IMainView Members

        public string GetCurrentConnectionString
        {
            get { return dbOpenFileDialog.FileName; }
        }

        public List<DocumentHeader> SetDocumentHeaders
        {
            set
            {
                ((CustomBindingList<DocumentHeader>) DocumentHeaderBindingSource.DataSource).SetElements(value);
            }
        }

        public List<Inventory> SetInventories
        {
            set
            {
                ((CustomBindingList<Inventory>)InventoryBindingSource.DataSource).SetElements(value);
            }
        }
        
        public DocumentHeader CurrentDocumentHeader
        {
            get
            {
                return (DocumentHeader)DocumentHeaderBindingSource.Current;
            }
        }

        public Inventory CurrentInventory
        {
            get { return (Inventory)InventoryBindingSource.Current; }
        }

        public string SortedColumn
        {
            get
            {
                if (inventoryDataGridView.SortedColumn == null)
                    return string.Empty;
                return inventoryDataGridView.SortedColumn.DataPropertyName;//sortColumn.DataPropertyName; }
            }
        }

        public string SortedType
        {
            get
            {
                return inventoryDataGridView.SortOrder == SortOrder.Ascending ? "Asc" : "Desc";//sortDirection == ListSortDirection.Ascending ? "Asc" : "Desc";
            }
        }

        public int Page
        {
            get
            {
                return _currentPage;
            }
            set
            {
                _currentPage = value;
                bindingNavigatorPositionPage.Text = value.ToString();
                LoadInventors(null, null);
                if (Page == _countPage)
                {
                    bindingNavigatorMoveNextPage.Enabled = false;
                    bindingNavigatorMoveLastPage.Enabled = false;
                }
                else
                {
                    bindingNavigatorMoveNextPage.Enabled = true;
                    bindingNavigatorMoveLastPage.Enabled = true;
                }
                if (Page == 1)
                {
                    bindingNavigatorMovePreviousPage.Enabled = false;
                    bindingNavigatorMoveFirstPage.Enabled = false;
                }
                else
                {
                    bindingNavigatorMovePreviousPage.Enabled = true;
                    bindingNavigatorMoveFirstPage.Enabled = true;
                }
            }

        }

        public int CountPage
        {
            set
            {
                _countPage = value;
                bindingNavigatorCountPage.Text = "of{" + _countPage + "}";
            }
        }

        public int PageSize
        {
            get { return pageSize; }
        }

        public event EventHandler ChangeDatabase;
        public event EventHandler LoadInventors;
        public event EventHandler AddInventory;
        public event EventHandler EditInventory;
        public event EventHandler DeleteInventory;

        #endregion

        private void documentHeaderDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            throw new Exception("Error on bindidng DatGrid");
        }
    }
}
